/**
  ******************************************************************************
  * @file    USART/HyperTerminal_Interrupt/main.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    18-May-2012
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_misc.h"
#include "stm32f0xx_usart.h"
#include "..\UserLib\include\typedef.h"
#include "..\UserLib\include\bitopt.h"
#include "..\UserLib\include\fifo.h"
#include "..\UserLib\include\serial.h"
#define TXBUSY 0x01
#define TX_FIFO_MAX 0x80
#define RX_FIFO_MAX 0x80

/** @addtogroup STM32F0xx_StdPeriph_Examples
  * @{
  */

/** @addtogroup HyperTerminal_Interrupt
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/


/* Private macro -------------------------------------------------------------*/
#define countof(a)   (sizeof(a) / sizeof(*(a)))

USART_InitTypeDef USART_InitStructure;

 uint8  serialTxBuf[TX_FIFO_MAX];
 Fifo  serialTxFifo;
 uint8 txState;
 uint8 serialRxBuf[TX_FIFO_MAX];
static Fifo serialRxFifo;

/* Private function prototypes -----------------------------------------------*/
void NVIC_Config(void);
void USARTInit(uint32_t baud);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void SerialPort_Init(int32_t baud)
{

  NVIC_Config();
  
  USARTInit(baud);
  
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  



}

void Serial_SendArr(void * buf,uint8 len)
{
	uint8 i;
	for(i=0;i<len;i++)
	{
		Serial_Send(((uint8 *)buf)[i]);
	}
}

/**
  * @brief  Configures the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USART2 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  Configures COM port.
  * @retval None
  */
void USARTInit(uint32_t baud)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
	Fifo_Init(&serialTxFifo,serialTxBuf,TX_FIFO_MAX);
	Fifo_Init(&serialRxFifo,serialRxBuf,RX_FIFO_MAX);
    
    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    /* Enable GPIO clock */
//    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    /* Enable USART clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    
    /* Connect PXx to USARTx_Tx */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);

    /* Connect PXx to USARTx_Rx */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
    
    /* Configure USART Tx, Rx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* USART configuration */
    USART_Init(USART1, &USART_InitStructure);

    /* Enable USART */
    USART_Cmd(USART1, ENABLE);

}

void Serial_Send(uint8 sendChar)
{

   if(!Fifo_CheckFull(&serialTxFifo))
   {
			Fifo_WriteFifo(&serialTxFifo,sendChar);
    }
	if(!CHKBIT(txState,TXBUSY))
    {
      txState=TXBUSY;
      USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
    }
}

uint8 Serial_Recive(unsigned char *readChar)
{
	return Fifo_ReadFifo(&serialRxFifo,readChar);

}

/**
  * @brief  This function handles USART1 global interrupt request.
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
      Fifo_WriteFifo(&serialRxFifo,USART_ReceiveData(USART1));
      //RxBuffer[RxCount++] = ((USART1) & 0x7F);
  }

  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {
    /* Write one byte to the transmit data register */
       uint8 temp;
		if(Fifo_CheckNoEmpty(&serialTxFifo))
		{
			Fifo_ReadFifo(&serialTxFifo,&temp);
            USART_SendData(USART1, temp);

		}
		else
		{
			CLRBIT(txState,TXBUSY);
            USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

		}
  }
}

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
